import router from "@/router";
import { ElMessage } from "element-plus";
import { useBreadcrumbStore } from "@/stores/breadcrumb.js";
const breadcrumb = useBreadcrumbStore();
// 白名单，里面是路由对象的name
const WhiteList = ["/", "/login"];

router.beforeEach(async (to, from) => {
  // if (WhiteList.includes(to.path)) {
  //   return true;
  // } else {
  //   console.log(sessionStorage.getItem("isTrue"));
  //   if (!sessionStorage.getItem("isTrue")) {
  //     ElMessage({
  //       message: "登录之后才能查看该页面！",
  //       type: "warning",
  //       duration: 3000,
  //     });
  //     return from.path;
  //   } else {
  //     return true;
  //   }
  // }
  // getBreadcrumbList(to.name, router.getRoutes());
});

// 获取当前的面包屑数组
function getBreadcrumbList(currentName, routes) {
  const titles = [];
  while (true) {
    const currentRouteObj = routes.find((item) => item.name == currentName);
    let obj = {
      name: currentRouteObj.name,
      title: currentRouteObj.meta.title,
    };
    titles.push(obj);
    const parentRouterObj = routes.find(
      (item) => item.name == currentRouteObj.meta.parentRouter
    );
    if (parentRouterObj) {
      currentName = parentRouterObj.name;
      continue;
    } else {
      break;
    }
  }
  titles.pop();
  breadcrumb.changeBreadcrumbList(titles.reverse());
}
